var nowDate = getDay(0);
var calendar = new lCalendar();
var starttimecalendar = new lCalendar();
var endtimecalendar = new lCalendar();
starttimecalendar.init({
	'trigger': '#startDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
endtimecalendar.init({
	'trigger': '#endDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
function ICalendarClick(){

//	onload()
}
var dataJson;
$(document).ready(function(){
	var date =new Date()
	var year=date.getFullYear();
	var month=date.getMonth()+1;
	var theday = date.getDate(); 
	if(month>=0&&month<=9){month='0'+month}
	var lastDay=new Date(year,month,0).getDate();
	$('#name').text(sessionStorage.getItem("name"));
	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
	document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
//	$('#startDate').val(nowDate);
//	$('#endDate').val(nowDate)
	
	$('#searchButton').on('click',function(){
		if($('#startDate').val() > $('#endDate').val()){
			alert("开始日期不能大于结束日期")
		}else{
			getData();
		}
	});
	$('#planuserSelect').on('change',setTableList);
	$('#plantypeSelect').on('change',setTableList);
	$('#flagSelect').on('change',setTableList);
	getData();
	$('#custom').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="";//隐藏
	});
	$('#month').on('click',function(){
			document.getElementById("renyiriqibox").style.display="none";//隐藏
		   	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
			document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
 			getData(); 
	});
	$('#week').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="none";//隐藏
		     document.getElementById("startDate").value=getCurrentWeekFirstDay(year+'-'+month+'-'+theday);
			 document.getElementById("endDate").value=getCurrentWeekLastDay(year+'-'+month+'-'+theday);
		     getData();
	});
	
});
function getData(){
	showloading();
	var sendData = {};
	sendData.planstartDate = $('#startDate').val();
	sendData.planendDate = $('#endDate').val();
	sendData.planuserid = sessionStorage.getItem("userId");
	sendData.commituserid = sessionStorage.getItem("userId");
	sendData.checkuserid = sessionStorage.getItem("userId");
	sendData.backuserid = sessionStorage.getItem("userId");
	sendData.lrrid = sessionStorage.getItem("userId");
	$.ajax({
		url: "../../work_plan_info/getBody"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res);
			try {
				//插入计划人
				var planuserJson = res.planuserJson; 
				$('#planuserSelect').empty();
				$('#planuserSelect').append('<option value="">责任人</option>');
				for(var el in planuserJson){
					$('#planuserSelect').append('<option value="'+el+'">'+planuserJson[el]+'</option>');
				}
				//循环表格数据
				dataJson = res.data;
				setTableList(dataJson);
				//console.log(res);
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
}

function setTableList(){
	$('#tableListDiv div[tableDiv]').remove();
	var planuser = $('#planuserSelect').val();
    var plantypeselect = $('#plantypeSelect').val();
    var flagselect = $('#flagSelect').val();
    var text=''; 
	for(var el in dataJson){//获取每日的Json
		var dayJson = dataJson[el];
		for(var obj in dayJson){ //获取每人Json
			if(planuser == '' || planuser == obj){//切换下拉框
				var userJson = dayJson[obj];
				var trStr = '';
				var planStr = '';
				for (var i = 0; i < userJson.length; i++) {
					if(userJson[i].jindu==undefined){userJson[i].jindu='0%'}
					var imgStr = userJson[i].img;
					var imgTd = "";
					if(imgStr != undefined && imgStr != ''){
						imgTd = '查看';
					}
					if(userJson[i].flag==12){userJson[i].flag='未回复'; thetime=userJson[i].replydate;}
					else if(userJson[i].flag==10){userJson[i].flag='未完成'; thetime=userJson[i].enddate}
					else if(userJson[i].flag==9){userJson[i].flag='已完成';thetime=userJson[i].completedate}
					else if(userJson[i].flag==500){userJson[i].flag='已回复';thetime=userJson[i].replydate}
					if(thetime==undefined){thetime = '';}
    				if(userJson[i].plantype==2&&(plantypeselect==1||plantypeselect==3)&&(userJson[i].flag==flagselect||flagselect=='')){
					trStr += '<tr class="thin-border-bottom">'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;"><span class="num">'+(i+1)+'</span></td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" class="orange" id="'+userJson[i].pno+'" plantype="'+userJson[i].plantype+'">'+userJson[i].content+'</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+userJson[i].flag+'</td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+thetime+'</td>'
							+'</tr>'
					}else if(userJson[i].plantype==1&&(plantypeselect==2||plantypeselect==3)&&(userJson[i].flag==flagselect||flagselect=='')){
						planStr += '<tr class="thin-border-bottom">'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;"><span class="num">'+(i+1)+'</span></td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" class="orange" id="'+userJson[i].pno+'" plantype="'+userJson[i].plantype+'">'+userJson[i].content+'</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+userJson[i].flag+'</td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+thetime+'</td>'
							+'</tr>'
					}	
					
					if(userJson[i].plantype==2){
						if(userJson[i].meetingtheme==undefined){
							text = '';
						}else{
							text = userJson[i].meetingtheme + ' 月工作内容';
						}
					}else{
						 text = userJson[i].planuser +  ' 的日工作内容'
					}	
				}
				if(trStr!=''||planStr!=''){
					//if(userJson[0].planuser.length>=7){userJson[0].planuser = userJson[0].planuser.substring(0,5)+'...'}
		
				$('#tableListDiv').append('<div tableDiv>'
										+'	<div class="widget-box">'
										+'	<div class="widget-header widget-header-flat widget-header-small" style="">'
							//			+'		<h5>'+userJson[0].planuser+'工作内容</h5>'
										+'		<h5 style="">'+text+'</h5>'
										+'       <div class="widget-toolbar" id="shangpinmore">'
										+'			<span style="">'+userJson[0].plandate+'</span>'
										+'		</div>'
										+'	</div>'
										+'	<div class="widget-body">'
										+'		<div class="widget-main">'
										+'			<div class="clearfix">'
										+'				<div class="widget-main no-padding">'
										+'					<table class="table tabl	e-bordered table-striped">'
										+'						<thead class="thin-border-bottom">'
										+'							<tr>'
										+'								<th style="width: 9%;;padding-left: 0px;padding-right: 0px;">序号</th>'
										+'								<th style="width: 45%;;padding-left: 0px;padding-right: 0px;">事项</th>'
										+'								<th style="width: 20%">状态</th>'
										+'								<th>时间</th>'
										+'							</tr>'
										+'						</thead>'
										+'						<tbody>'
										+							trStr+planStr
										+'						</tbody>'
										+'					</table>'
										+'				</div>'
										+'			</div>'
										+'		</div>'
										+'	</div>'
										+'</div>'
										+'</div>');
										
				}						
										
										
				userJson = null;
			}
		}
		dayJson = null;
	}
	//点击显示详情
	$("tbody td[id]").on("click",null,function(){
		var html = $(this).html();
		var id = $(this).attr("id");
		var ptype = $(this).attr("plantype");
		if(ptype==1){
			url = "work_plan_plansearch?pno="+id;//此处拼接内容
	        window.location.href = url;
		}else{
			url = "work_plan_search?pno="+id;//此处拼接内容
	        window.location.href = url;
		}
		/*var value = $(this).text();
		if(value != undefined){
			//tips层-上
			layer.tips(value, '#'+id, {
				tips: [1, '#0FA6D8'] //还可配置颜色
			});	
		}*/
		
	});
}

//预览图片
function showAllPic(imgStr){
	photoUrl = imgStr.split(',');
	dd.biz.util.previewImage({
		urls: photoUrl,//图片地址列表
		current: photoUrl[0],//当前显示的图片链接
		onSuccess : function(result) {
			/**/
			
		},
		onFail : function(err) {}
	})
}
